package com.wql.rabbitmq.study;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.QueueingConsumer;
import com.wql.rabbitmq.Constant;

public class DirectTestForConsumer {

	public static void main(String[] args) throws Exception {
		for (int i = 0; i < 3; i++) {
			final int index = i;
			new Thread(new Runnable() {

				@Override
				public void run() {
					try {
						consumer(index);
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
			}).start();
		}
	}

	private static void consumer(int index) throws IOException, TimeoutException, InterruptedException {
		ConnectionFactory factory = new ConnectionFactory();
		factory.setHost(Constant.IP);
		Connection conn = factory.newConnection();
		Channel channel = conn.createChannel();
		channel.queueDeclare(Constant.QUEUE_NAME, false, false, false, null);
		QueueingConsumer consumer = new QueueingConsumer(channel);
		channel.basicConsume(Constant.QUEUE_NAME, true, consumer);
		while (true) {
			QueueingConsumer.Delivery delivery = consumer.nextDelivery();
			String msg = new String(delivery.getBody());
			System.out.println(index + "收到消息：" + msg);
		}
	}

}
